import NProgress from 'nprogress'
import { getCookie } from "@/config/cookie";
import 'nprogress/nprogress.css'


NProgress.configure({
    showSpinner: false
})

const whiteList = ["/login"]

export const routerDefaultConf = {
    waitForData: true,
    scrollBehavior: () => ({ y: 0 }),
    transitionOnLoad: true,
    linkActiveClass: "active"
}

export async function routerBeforeEachFunc(to, from, next) {
    NProgress.start()
    const hasToken = getCookie('isLogin')

    if (hasToken) {

        if (to.path === '/login') {
            // if is logged in, redirect to the home page
            next({ path: '/' })
            NProgress.done()
        } else {
            next()
            NProgress.done()
        }
    } else {
        /* has no token*/

        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {

            next({
                path: "/login"
            })
            NProgress.done()
        }
    }
}

export function routerAfterFunc() {
    NProgress.done()
}